﻿using System;
using RpcHelper;
using RpcModularModel.Identity;
using RpcModularModel.Identity.Model;

namespace RpcModular.Model
{
    /// <summary>
    /// 用户标识
    /// </summary>
    internal class UserIdentity : DataSyncClass
    {
        internal UserIdentity(string identityId)
        {
            this.IdentityId = identityId;
        }
        /// <summary>
        /// 标识ID
        /// </summary>
        public string IdentityId
        {
            get;
            private set;
        }
        /// <summary>
        /// 应用名称
        /// </summary>
        public string AppName
        {
            get;
            private set;
        }
        /// <summary>
        /// 是否验证权限
        /// </summary>
        public bool IsValid
        {
            get;
            private set;
        }
        /// <summary>
        /// 是否检查网关请求路径权限
        /// </summary>
        private bool _IsCheckPath = false;
        /// <summary>
        /// 允许的网关路径
        /// </summary>
        private string[] _LocalPath = null;

        private bool _IsCheckRoute = false;

        private string[] _RouteList = null;


        protected override void SyncData()
        {
            IdentityDatum datum = new GetIdentity
            {
                IdentityId = new Guid(this.IdentityId)
            }.Send();
            this.AppName = datum.AppName;
            this._LocalPath = datum.Path;
            this._RouteList = datum.Routes;
            this.IsValid = datum.IsValid;
            this._IsCheckPath = !datum.Path.IsNull();
            this._IsCheckRoute = !datum.Routes.IsNull();
        }

        internal bool CheckRoute(string msgKey)
        {
            if (!this._IsCheckRoute)
            {
                return true;
            }
            else
            {
                return this._RouteList.IsExists(msgKey);
            }
        }

        public bool CheckGateway(string path)
        {
            if (this._IsCheckPath)
            {
                return true;
            }
            else
            {
                return this._LocalPath.StartsWith(path);
            }
        }

    }
}
